﻿package Welsy4.Connect.commands {
	import com.adobe.cairngorm.commands.SequenceCommand;
	import flash.net.URLVariables;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import Welsy4.Connect.business.PermissionDelegate;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.Meeting;
	import Welsy4.Connect.vo.User;


	public class AllowUserToMeetingCommand extends SequenceCommand implements Command, IResponder {

		override public function execute(event : CairngormEvent) : void {
			ConnectModel.getInstance().addQueue("AllowUserToMeetingCommand.execute ");
			this.nextEvent = ChainEvent(event).nextChainedEvent;

			var allowPermissionRequest : URLVariables = ChainEvent(event).eventRequest;

			/* set request GET params */
			allowPermissionRequest.decode("principal-id=" + ConnectModel.getInstance().currentUser.account_id);
			allowPermissionRequest.action = "permissions-update";
			allowPermissionRequest.decode("acl-id=" + ConnectModel.getInstance().lastMeetingCreated.sco_id);
			allowPermissionRequest.session = ConnectModel.getInstance().sessionCookie;
			allowPermissionRequest.nocache = User.getLoginRequestlocaleTime();
			trace("Request:\n" + allowPermissionRequest.toString());
			/* set Delegate to send data*/
			var permissionDelegate : PermissionDelegate = new PermissionDelegate(this);
			permissionDelegate.setPermission(allowPermissionRequest);

		}

		public function result(event : Object) : void {

			// listen for ok result
			//AG elimianto!
			/* CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.USER_ALLOWED_TO_MEETING,true,ResponseEvent.OK, event.result)); */
			this.executeNextCommand();
			ConnectModel.getInstance().addQueue("AllowUserToMeetingCommand.result " + event.result);

		}

		public function fault(event : Object) : void {
			ConnectModel.getInstance().addQueue("AllowUserToMeetingCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}
	}

}